/*
 * usart.h
 *
 *  Created on: 2020年12月28日
 *      Author: Administrator
 */

#ifndef USER_DRIVER_USART_H_
#define USER_DRIVER_USART_H_

//USART0///////////////////////////////////////////////////////////////////////////
//#define USART0_RX_INT_BIT   (1 << USART0_RX_PIN)
#define USART0_TX_PORT      gpioPortC
#define USART0_TX_PIN       10
#define USART0_RX_PORT      gpioPortC
#define USART0_RX_PIN       11
#define USART0_BAUDRATE     115200
#define USART0_RX_IRQ       false
#define USART0_TX_IRQ       true
#define USART0_ROUTELOC     USART_ROUTELOC0_RXLOC_LOC15 | USART_ROUTELOC0_TXLOC_LOC15
#define USART0_ROUTEPEN     USART_ROUTEPEN_TXPEN | USART_ROUTEPEN_RXPEN

//USART1///////////////////////////////////////////////////////////////////////////
//#define USART1_RX_INT_BIT   (1 << USART1_RX_PIN)
#define USART1_TX_PORT      gpioPortA
#define USART1_TX_PIN       0
#define USART1_RX_PORT      gpioPortA
#define USART1_RX_PIN       1
#define USART1_BAUDRATE     115200
#define USART1_RX_IRQ       false
#define USART1_TX_IRQ       true
#define USART1_ROUTELOC     USART_ROUTELOC0_RXLOC_LOC0 | USART_ROUTELOC0_TXLOC_LOC0
#define USART1_ROUTEPEN     USART_ROUTEPEN_TXPEN | USART_ROUTEPEN_RXPEN

#define IDLE    0
#define BUSY    1

//ldma//////////////////////////////////////////////////////////////////////////////

#define LDMA_USART0_RX_CHANNEL                      0
#define LDMA_USART0_RX_CH_MASK                      (1 << LDMA_USART0_RX_CHANNEL)

#define LDMA_USART0_TX_CHANNEL                      1
#define LDMA_USART0_TX_CH_MASK                      (1 << LDMA_USART0_TX_CHANNEL)

#define LDMA_USART1_RX_CHANNEL                      2
#define LDMA_USART1_RX_CH_MASK                      (2 << LDMA_USART1_RX_CHANNEL)

#define LDMA_USART1_TX_CHANNEL                      3
#define LDMA_USART1_TX_CH_MASK                      (3 << LDMA_USART1_TX_CHANNEL)

struct dma_map_t
{
    uint32_t index;
    uint16_t lens;
};

struct dma_store_info_t
{
    struct dma_map_t map;
    struct dma_map_t map2;
};

struct dma_recv_instance{
    lwrb_t rb;
    uint8_t * dma_buff;
    int dma_len;
    uint8_t ch;
    volatile uint32_t index_offset;
};

struct dma_send_instance{
    StreamBufferHandle_t    stream;
    uint8_t ch;
    uint8_t transmit_status;
};


int ldmaMsgStore(lwrb_t* rb, uint8_t ch, volatile uint32_t *record);
void dma_usart_recv_instance_init(struct dma_recv_instance *instance, uint8_t *buff, int size, uint8_t *dma_buff, uint8_t dma_ch);

void dma_usart_send_instance_init(struct dma_send_instance *instance, int buffer_size, int trigger_level, uint8_t dma_ch);
#endif /* USER_DRIVER_USART_H_ */
